<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div v-if="article" class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ article.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="article.author_id" :label="article.pubdate">
        <template #icon>
          <!-- 头像 -->
          <img :src="article.cover_img" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <!-- 是否关注了作者 -->
            <van-button
              v-if="article.is_followed"
              type="info"
              size="mini"
              @click="cancelFollow"
              >已关注</van-button
            >
            <van-button
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              @click="handleFollow"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div
        v-if="article.content && !article.content.startsWith('http')"
        class="art-content"
        v-html="article.content"
      ></div>
      <video
        v-if="article.content && article.content.startsWith('http')"
        width="100%"
        controls
        preload="auto"
        src="../../assets/mda-pir51htc4ugr3t8y.mp4"
      ></video>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          v-if="article.attitude === 1"
          icon="good-job"
          type="danger"
          size="small"
          @click="deleteLike"
          >已点赞</van-button
        >
        <van-button
          v-else
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          @click="setLike"
          >点赞</van-button
        >
      </div>
    </div>

    <!-- 文章的评论组件 -->
    <ArtCmt :art-id="$route.params.id"></ArtCmt>
  </div>
</template>

<script>
// 导入文章的评论组件
import ArtCmt from "@/components/ArtCmt/ArtCmt.vue";
import {
  getArticleDetail,
  setFollow,
  cancelFollow,
  setLike,
  deleteLike,
} from "@/api/request.js";
export default {
  name: "Detail",
  beforeRouteLeave(to, from, next) {
    from.meta.top = window.scrollY;
    setTimeout(() => {
      next();
    }, 0);
  },
  props: {
    // 文章的id
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      // 文章的信息
      article: {},
    };
  },
  components: {
    ArtCmt,
  },
  created() {
    this.getArticleDetail();
  },
  methods: {
    // 获取文章详情数据
    async getArticleDetail() {
      const params = {
        id: this.$route.params.id,
      };
      const { data: res } = await getArticleDetail(params);
      const { status, message, data } = res;
      if (status !== 0) {
        return this.$notify({ type: "danger", message: message });
      }
      // this.$notify({ type: "success", message: "获取文章信息成功" });
      this.article = data[0];
    },
    // 点击关注按钮
    handleFollow() {
      this.setFollow();
    },
    // 关注作者
    async setFollow() {
      const data1 = {
        target: this.article.aut_id,
      };
      const { data: res } = await setFollow(data1);
      // console.log(res);
      const { message, data } = res;
      if (message !== "OK") {
        return this.$notify({ type: "danger", message: "关注失败" });
      }
      this.$notify({ type: "success", message: "关注成功" });
      data.target
        ? (this.article.is_followed = true)
        : (this.article.is_followed = false);
    },
    // 取消关注作者
    async cancelFollow() {
      const res = await cancelFollow(this.article.aut_id.toString());
      // console.log(res);
      if (res.status !== 204) {
        this.$notify({ type: "danger", message: "取关失败请稍后重试" });
      }
      this.$notify({ type: "success", message: "取关成功" });
      this.article.is_followed = false;
    },
    // 点赞文章
    async setLike() {
      const data = {
        target: this.$route.params.id,
      };
      const { data: res } = await setLike(data);
      // console.log(res);
      if (res.message !== "OK") {
        this.$notify({ type: "danger", message: "点赞失败请稍后重试" });
      }
      this.article.attitude = 1;
      this.$notify({ type: "success", message: "点赞成功" });
    },
    // 取消点赞
    async deleteLike() {
      const data = {
        article_id: this.$route.params.id,
      };
      const res = await deleteLike(data);
      // console.log(res);
      if (res.status !== 204) {
        this.$notify({ type: "danger", message: "取消失败请稍后重试" });
      }
      this.article.attitude = -1;
      this.$notify({ type: "success", message: "取消成功" });
    },
  },
};
</script>

<style lang="less" scoped>
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

.van-nav-bar {
  /deep/.van-icon {
    color: #fff !important;
  }
}
</style>

<style>
.hljs {
  display: block;
  overflow-x: auto;
  padding: 0.5em;
  color: #444;
  background: #f0f0f0;
}
</style>
